package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.NewsDTO;
import com.movie.dto.PageResponse;
import com.movie.service.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资讯控制器
 */
@RestController
@RequestMapping("/news")
@RequiredArgsConstructor
@Tag(name = "资讯管理", description = "资讯信息的增删改查接口")
public class NewsController {

    private final NewsService newsService;

    /**
     * 获取资讯列表
     */
    @GetMapping
    @Operation(summary = "分页获取资讯列表", description = "支持分页、分类、状态、置顶筛选")
    public ApiResponse<PageResponse<NewsDTO>> getNewsList(
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Boolean isPublished,
            @RequestParam(required = false) Boolean isTop,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "time") String sort) {

        PageResponse<NewsDTO> newsList = newsService.getNewsList(category, isPublished, isTop, page, size, sort);
        return ApiResponse.success("获取资讯列表成功", newsList);
    }

    /**
     * 获取资讯详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取资讯详情", description = "获取资讯详细信息")
    public ApiResponse<NewsDTO> getNewsDetail(@PathVariable Long id) {
        NewsDTO newsDetail = newsService.getNewsDetail(id);
        return ApiResponse.success("获取资讯详情成功", newsDetail);
    }

    /**
     * 搜索资讯
     */
    @GetMapping("/search")
    @Operation(summary = "模糊搜索资讯信息", description = "模糊搜索资讯信息")
    public ApiResponse<PageResponse<NewsDTO>> searchNews(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        PageResponse<NewsDTO> searchResult = newsService.searchNews(keyword, page, size);
        return ApiResponse.success("搜索资讯成功", searchResult);
    }

    /**
     * 获取热门资讯
     */
    @GetMapping("/hot")
    @Operation(summary = "获取热门资讯", description = "获取热门资讯")
    public ApiResponse<List<NewsDTO>> getHotNews(
            @RequestParam(defaultValue = "5") int limit) {

        List<NewsDTO> hotNews = newsService.getHotNews(limit);
        return ApiResponse.success("获取热门资讯成功", hotNews);
    }

    /**
     * 获取置顶资讯
     */
    @GetMapping("/top")
    @Operation(summary = "获取置顶资讯", description = "获取置顶资讯")
    public ApiResponse<List<NewsDTO>> getTopNews() {
        List<NewsDTO> topNews = newsService.getTopNews();
        return ApiResponse.success("获取置顶资讯成功", topNews);
    }

    /**
     * 获取最新资讯
     */
    @GetMapping("/latest")
    @Operation(summary = "获取最新资讯", description = "获取最新资讯")
    public ApiResponse<List<NewsDTO>> getLatestNews(
            @RequestParam(defaultValue = "5") int limit) {

        List<NewsDTO> latestNews = newsService.getLatestNews(limit);
        return ApiResponse.success("获取最新资讯成功", latestNews);
    }

    /**
     * 创建资讯
     */
    @PostMapping
    @Operation(summary = "创建资讯", description = "创建新的资讯")
    public ApiResponse<NewsDTO> createNews(@RequestBody NewsDTO newsDTO) {
        NewsDTO createdNews = newsService.createNews(newsDTO);
        return ApiResponse.success("创建资讯成功", createdNews);
    }

    /**
     * 更新资讯
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新资讯", description = "根据ID更新资讯信息")
    public ApiResponse<NewsDTO> updateNews(
            @PathVariable Long id, 
            @RequestBody NewsDTO newsDTO) {
        NewsDTO updatedNews = newsService.updateNews(id, newsDTO);
        return ApiResponse.success("更新资讯成功", updatedNews);
    }

    /**
     * 删除资讯
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除资讯", description = "根据ID删除资讯")
    public ApiResponse<Void> deleteNews(@PathVariable Long id) {
        newsService.deleteNews(id);
        return ApiResponse.success("删除资讯成功", null);
    }

    /**
     * 更新资讯状态
     */
    @PatchMapping("/{id}/status")
    @Operation(summary = "更新资讯状态", description = "更新资讯的发布状态")
    public ApiResponse<NewsDTO> updateNewsStatus(
            @PathVariable Long id, 
            @RequestBody StatusRequest statusRequest) {
        NewsDTO updatedNews = newsService.updateNewsStatus(id, statusRequest.getStatus());
        return ApiResponse.success("更新资讯状态成功", updatedNews);
    }

    /**
     * 设置置顶状态
     */
    @PatchMapping("/{id}/top")
    @Operation(summary = "设置置顶状态", description = "设置或取消资讯的置顶状态")
    public ApiResponse<NewsDTO> setTopNews(
            @PathVariable Long id, 
            @RequestBody TopRequest topRequest) {
        NewsDTO updatedNews = newsService.setTopNews(id, topRequest.getIsTop());
        return ApiResponse.success("设置置顶状态成功", updatedNews);
    }

    /**
     * 状态请求对象
     */
    public static class StatusRequest {
        private Boolean status;

        public Boolean getStatus() {
            return status;
        }

        public void setStatus(Boolean status) {
            this.status = status;
        }
    }

    /**
     * 置顶请求对象
     */
    public static class TopRequest {
        private Boolean isTop;

        public Boolean getIsTop() {
            return isTop;
        }

        public void setIsTop(Boolean isTop) {
            this.isTop = isTop;
        }
    }
}